//
//  UIImageExtensions.swift
//  ShimaPlayer
//
//  Created by michael on 2025/7/9.
//

import UIKit

extension UIImage {
    // MARK: - 纯色图片生成
    /// 根据颜色生成纯色图片
    /// - Parameters:
    ///   - color: 目标颜色
    ///   - size: 图片尺寸（默认1x1像素）
    static func fromColor(_ color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage? {
        let rect = CGRect(origin: .zero, size: size)
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        context.setFillColor(color.cgColor)
        context.fill(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image
    }

    // MARK: - 渐变图片生成（支持变换）
        /// 创建带变换的线性渐变图片
        /// - Parameters:
        ///   - colors: 颜色数组（至少需要2个颜色）
        ///   - size: 图片尺寸
        ///   - startPoint: 渐变起点（相对坐标，0-1范围）
        ///   - endPoint: 渐变终点（相对坐标，0-1范围）
        ///   - locations: 颜色位置数组（可选）
        ///   - transform: 要应用的CATransform3D变换（可选）
        static func gradientImage(
            colors: [UIColor?],
            size: CGSize,
            startPoint: CGPoint = CGPoint(x: 0.0, y: 0.5),
            endPoint: CGPoint = CGPoint(x: 1.0, y: 0.5),
            locations: [CGFloat]? = nil,
            transform: CATransform3D? = nil
        ) -> UIImage? {
            guard colors.count >= 2 else { return nil }
            
            let rect = CGRect(origin: .zero, size: size)
            UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
            
            guard let context = UIGraphicsGetCurrentContext() else { return nil }
            context.saveGState()
            
            // 应用变换（如果有）
            if let transform = transform {
                let affineTransform = CATransform3DGetAffineTransform(transform)
                context.concatenate(affineTransform)
            }
            
            // 创建渐变
            let cgColors = colors.map { $0?.cgColor }
            guard let gradient = CGGradient(
                colorsSpace: CGColorSpaceCreateDeviceRGB(),
                colors: cgColors as CFArray,
                locations: locations
            ) else { return nil }
            
            // 计算实际坐标点
            let actualStartPoint = CGPoint(
                x: startPoint.x * size.width,
                y: startPoint.y * size.height
            )
            let actualEndPoint = CGPoint(
                x: endPoint.x * size.width,
                y: endPoint.y * size.height
            )
            
            // 绘制渐变
            context.drawLinearGradient(
                gradient,
                start: actualStartPoint,
                end: actualEndPoint,
                options: [.drawsBeforeStartLocation, .drawsAfterEndLocation]
            )
            
            let image = UIGraphicsGetImageFromCurrentImageContext()
            context.restoreGState()
            UIGraphicsEndImageContext()
            
            return image
        }
    
    // 便捷方法：创建垂直渐变
    static func verticalGradientImage(
        colors: [UIColor],
        size: CGSize,
        locations: [CGFloat]? = nil
    ) -> UIImage? {
        return gradientImage(
            colors: colors,
            size: size,
            startPoint: CGPoint(x: 0.5, y: 0.0),
            endPoint: CGPoint(x: 0.5, y: 1.0),
            locations: locations
        )
    }
    
    // 便捷方法：创建水平渐变
    static func horizontalGradientImage(
        colors: [UIColor],
        size: CGSize,
        locations: [CGFloat]? = nil
    ) -> UIImage? {
        return gradientImage(
            colors: colors,
            size: size,
            startPoint: CGPoint(x: 0.0, y: 0.5),
            endPoint: CGPoint(x: 1.0, y: 0.5),
            locations: locations
        )
    }
}
